import 'package:dropdown_below/dropdown_below.dart';
import 'package:flutter/material.dart';

//
// DropdownButton({
// Key key,
// @required this.items,       // 下拉选项列表
// this.selectedItemBuilder,   // 选项 item 构造器
// this.value,                 // 选中内容
// this.hint,                  // 启动状态下默认内容
// this.disabledHint,          // 禁用状态下默认内容
// @required this.onChanged,   // 选择 item 回调
// this.elevation = 8,         // 阴影高度
// this.style,                 // 选项列表 item 样式
// this.underline,             // 按钮下划线
// this.icon,                  // 下拉按钮图标
// this.iconDisabledColor,     // 禁用状态下图标颜色
// this.iconEnabledColor,      // 启动时图标颜色
// this.iconSize = 24.0,       // 图标尺寸
// this.isDense = false,       // 是否降低按钮高度
// this.isExpanded = false,    // 是否将下拉列表内容设置水平填充
// })

class DropdownButtonWidget extends StatefulWidget {
  final String? text;

  DropdownButtonWidget({this.text}) : super();

  @override
  State<StatefulWidget> createState() {
    return DropdownState();
  }
}

class DropdownState extends State<DropdownButtonWidget> {
  List _testList = [
    {'no': 1, 'keyword': 'blue'},
    {'no': 2, 'keyword': 'black'},
    {'no': 3, 'keyword': 'red'}
  ];
  List<DropdownMenuItem<Object>> _dropdownTestItems = [];
  var _selectedTest;

  @override
  void initState() {
    _dropdownTestItems = buildDropdownTestItems(_testList);
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  List<DropdownMenuItem<Object>> buildDropdownTestItems(List _testList) {
    List<DropdownMenuItem<Object>> items = [];
    for (var i in _testList) {
      items.add(
        DropdownMenuItem(
          value: i,
          child: Text(i['keyword']),
        ),
      );
    }
    return items;
  }

  onChangeDropdownTests(selectedTest) {
    print(selectedTest);
    setState(() {
      _selectedTest = selectedTest;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('DropdownBelow '),
      ),
      body: Center(
        child: Column(
          children: [
            SizedBox(
              height: 150,
            ),
            DropdownBelow(
              boxDecoration: BoxDecoration(
                //背景渐变
                border: Border.all(color: Colors.red, width: 1), //边框
                borderRadius: BorderRadius.circular(3.0),
              ),
              itemWidth: 100,
              itemTextstyle: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w400,
                  color: Colors.black),
              boxTextstyle: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w400,
                  color: Color(0XFFbbbbbb)),
              boxPadding: EdgeInsets.fromLTRB(13, 12, 0, 12),
              boxWidth: 100,
              boxHeight: 45,
              isDense: false,
              icon: Icon(Icons.arrow_drop_down),
              hint: Text('请选择数据'),
              value: _selectedTest,
              items: _dropdownTestItems,
              onChanged: onChangeDropdownTests,
            ),
            SizedBox(
              height: 50,
            ),
            DropdownBelow(
              boxDecoration: BoxDecoration(
                  border: Border(
                      bottom: BorderSide(width: 1, color: Colors.black))),
              itemWidth: 100,
              itemTextstyle: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w400,
                  color: Colors.black),
              boxTextstyle: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w400,
                  color: Color(0XFFbbbbbb)),
              boxPadding: EdgeInsets.fromLTRB(13, 12, 0, 12),
              boxWidth: 100,
              boxHeight: 45,
              isDense: false,
              icon: Icon(Icons.arrow_drop_down),
              hint: Text('请选择数据'),
              value: _selectedTest,
              items: _dropdownTestItems,
              onChanged: onChangeDropdownTests,
            ),
          ],
        ),
      ),
    );
  }
}
